package com.zlx.xBiaoShop.service;


import com.zlx.xBiaoShop.entity.Order;
import com.zlx.xBiaoShop.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description order 服务层
 * @author admin
 * @date 2020-12-05 17:52:12
*/
@Service
public class OrderService {

	@Autowired
	private OrderMapper orderMapper;

	/*
		添加购物车
	 */
	@Transactional
	public void addShoppingCart(Order order){
		Order isExitsOrder = orderMapper.selectOrderByUserIdAndGoodsId(order.getUserId(),order.getGoodsId());
		//商品未添加
		if (isExitsOrder==null) {
			order.setPayStaus(0);
			order.setRecvStatus(0);
			order.setCreateDate(new Date());
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			order.setOrderId(uuid);
//		System.out.println(order);
			orderMapper.addOrder(order);
		}else {
			//商品已添加
			isExitsOrder.setTotalMoney(isExitsOrder.getTotalMoney()+order.getTotalMoney());
			isExitsOrder.setGoodsCount(isExitsOrder.getGoodsCount()+order.getGoodsCount());
			orderMapper.updateTotalManeyAndCountById(isExitsOrder.getGoodsCount(),
					isExitsOrder.getTotalMoney(),isExitsOrder.getOrderId());
		}
	}
	/*
		获取所有的购物车商品列表
	 */
	public List<Order> getGoodsListByUserId(Integer userId) {
		List<Order> oderByUserId = orderMapper.getOderByUserId(userId);
//		Collections.reverse(oderByUserId);
		return oderByUserId;
	}

	/**
	 * 商品价格的变化
	 * @param orderId
	 * @param goodsCount
	 * @param price
	 */
	@Transactional
	public void changeGoodCount(String orderId, Integer goodsCount, Double price) {
		Double totalMoney = price*goodsCount;
		orderMapper.changeGoodCount(orderId,goodsCount,totalMoney);
	}

	/**
	 * 根据订单id删除订单
	 * @param orderId
	 */
	@Transactional
	public void dedeleteOderByOrderId(String orderId) {
		orderMapper.dedeleteOderByOrderId(orderId);
	}

	/**
	 * 修改支付状态：待支付--1
	 * @param orderId
	 */
	@Transactional
	public void toPayOder(String orderId) {
		orderMapper.updatePayStatusByOrderId(orderId);
	}

	/**
	 * 获取待支付状态的订单
	 * @param userId
	 * @return
	 */
    public List<Order> getWaitPayOrderByUserId(Integer userId) {
		List<Order> oderByUserId = orderMapper.getWaitPayOrderByUserId(userId);
		return oderByUserId;
    }
	/**
	 * 修改支付状态：已支付--2
	 * @param orderId
	 */
	@Transactional
	public void payOder(String orderId) {
		orderMapper.updateOrderPayStatusPayByOrderId(orderId);
	}

	/**
	 * 获取待收货状态的订单
	 * @param userId
	 * @return
	 */
	public List<Order> getPayedOrderByUserId(Integer userId) {
		List<Order> oderByUserId = orderMapper.getPayedOrderByUserId(userId);
		return oderByUserId;
	}

	/**
	 * 修改收货状态：已收货--1
	 * @param orderId
	 */
	@Transactional
	public void confirmReceipt(String orderId) {
		orderMapper.updateOrderRecvStatusByOrderId(orderId);
	}

	/**
	 * 获取已确认收货的订单
	 * @param userId
	 * @return
	 */
	public List<Order> getRecvOrderByUserId(Integer userId) {
		List<Order> oderByUserId = orderMapper.getRecvOrderByUserId(userId);
		return oderByUserId;
	}


	public Integer getCarCountByUserId(Integer userId) {
		return orderMapper.getCarCountByUserId(userId);
	}

	public Integer getOrderCountByUserId(Integer userId) {
		return orderMapper.getOrderCountByUserId(userId);
	}
}
